/**

 @author 石头哥哥
 </P>
 Date:   2014/12/17
 </P>
 Time:   21:12

 注解：

 */
package GameObject {
import flash.geom.Point;

import lzm.starling.STLConstant;

import starling.animation.IAnimatable;
import starling.core.Starling;
import starling.display.DisplayObject;
import starling.display.Image;
import starling.display.Sprite;
import starling.textures.Texture;

/**
 * 子弹对象
 */
public class Bullet extends Sprite implements IAnimatable {


    /**
     * 子弹移动速度
     */
    private var speed:int = 0;

    /**
     * 子弹贴图类型
     */
    private var type:int = 0;


    private var _hp:int = 1;

    /**
     * 可造成的伤害值
     */
    private var _hurt:int;

    public function get hurt():int {
        return _hurt;
    }

    /**
     *
     * @param speed
     * @param type     子弹类型
     * @param parent
     */
    public function Bullet(type:int, speed:int = 9, hurt:int = 1) {
        super();
        this.type = type;
        this.speed = speed;
        this._hurt = hurt;
    }

    /**
     * 初始子弹实例
     * 1. bullet1
     * 2. bullet2
     */
    public function init():void {
        var bulletTexture:Texture;
        if (type == 1) {
            bulletTexture = GameSystem.assetManager.getTexture("bullet1");
        } else if (type == 2) {
            bulletTexture = GameSystem.assetManager.getTexture("bullet2");
        }
        if (bulletTexture) {
            var image:Image = new Image(bulletTexture);
            addChild(image);// add bullet container
        } else {
            throw  Error("create bullet texture failed " + type);
        }
    }

    /**
     * 更新子弹位置
     * @param time
     */
    public function advanceTime(time:Number):void {
        this.y += speed;
    }


    /**
     * 碰撞检测    hp  update
     * @return
     */
    public function collision(target:DisplayObject):Boolean {
        var raid:int = Math.max(this.width, this.height) / 2 + Math.max(target.width, target.height) / 2;//半径之和
        if (Point.distance(new Point(this.x + this.width / 2, this.y + this.height / 2),
                new Point(target.x + target.width / 2, target.y + target.height / 2)) <= raid) {

            if (target is Hero) {
                var hero:Hero = target as Hero;
                hero.hp = hero.hp - _hp;
                trace("[Bullet]", "hero under the fire.hp:"+hero.hp);
            } else if (target is Enemy) {
                var enemy:Enemy = target as Enemy;
                enemy.hp = enemy.hp - _hp;



              //  trace("[Bullet]", "enemy under the fire. hp:"+enemy.hp);
            }

            //clear();    //移除
            this.visible = false;
            return true;
        }
        return false;
    }


    /**
     * 超出了视线
     * 移除显示层 和 动画
     * clear obj of this
     */
    public function destroy(dispose:Boolean = true):Boolean {
        if (_hp <= 0 || this.y > STLConstant.StageHeight + 100 || this.y < -100) {
            removeFromParent(dispose);
            Starling.juggler.remove(this);
            return true;
        }
        return false;
    }

    /**
     *
     * @param dispose
     */
    public function clear(dispose:Boolean = true):void {
        removeFromParent(dispose);
        Starling.juggler.remove(this);
        this.dispose();
    }

    public function get hp():int {
        return _hp;
    }
}
}
